﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace SmartFrame_DataCollect.Forms
{

    public partial class frmLogin : XtraForm
    {
        string hzyc_mes_mssqlStr = "Data Source="+ DataBaseHelper.IP + ";Initial Catalog="+ DataBaseHelper.CockpitDataBase + ";User Id="+ DataBaseHelper.SqlAccountName + ";Password="+ DataBaseHelper .SqlAccountPwd+ ""; //"mes.hzyc.org.cn; Initial Catalog = ST1_BSCockpit; User Id = sa; Password=hzyc)(*765";       //"mes.hzyc.org.cn; Initial Catalog = ST1_BSCockpit; User Id = sa; Password=hzyc)(*765";  //"mes.hzyc.org.cn; Initial Catalog = ST1_BSCockpit; User Id = sa; Password=hzyc)(*765";
        private frmMain form;

        public frmLogin(frmMain form = null)
        {
            InitializeComponent();
            if (form != null)
            {
                this.form = form;
            }
            this.tips.Text = "请输入企业代号";
            this.form = form;
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            Console.WriteLine("登录窗体加载完成！");
        }

        // 当 edtTo 输入框输入完成离开时触发
        private void edtTo_Leave(object sender, EventArgs e)
        {
            //定义code为企业代号
            string code = this.edtTo.Text;
            //获取EnterNo企业代号
            string sqlStringEnterNo = "select Ba_Account.AccountName,Ba_Enterprise.EnterName,Ba_Enterprise.EnterNo from  Ba_Account INNER JOIN Ba_Enterprise ON Ba_Account.EnterID = Ba_Enterprise.EnterID WHERE Ba_Enterprise.EnterNo=@EnterNo;";
            DataTable data = DbHelperSQL.Query(sqlStringEnterNo,hzyc_mes_mssqlStr, new SqlParameter[] { 
                new SqlParameter("EnterNo", code)
            }).Tables[0];

            int count = cb.Properties.Items.Count;
            for (int i = 0; i < count; i++)
            {
                cb.Properties.Items.RemoveAt(0);
            }
            cb.Text = "";
            if (data.Rows.Count > 0)
            {
                this.tips.Text = data.Rows[0]["EnterName"].ToString();
                foreach (DataRow row in data.Rows)
                {
                    cb.Properties.Items.Add(row["AccountName"].ToString());
                }
                cb.SelectedIndex = 0;  // 自动选择第一个项
            }
            else
            {
                this.tips.Text = "请输入有效的企业代号!";
            }
        }


        // 确认登录按钮事件

        private void sbOK_Click(object sender, EventArgs e)
        {
            string code = this.edtTo.Text;
            string type = this.cb.SelectedItem?.ToString();
            string username = this.ac.Text;
            string password = this.ps.Text;

            // 校验输入
            if (string.IsNullOrEmpty(code))
            {
                MessageBox.Show("请输入企业代号！");
                return;
            }
            if (string.IsNullOrEmpty(type))
            {
                MessageBox.Show("请选择套账类型！");
                return;
            }
            if (string.IsNullOrEmpty(username))
            {
                MessageBox.Show("请输入用户名！");
                return;
            }
            if (string.IsNullOrEmpty(password))
            {
                MessageBox.Show("请输入密码！");
                return;
            }
            //登录验证过程
            string SqlString = "SELECT ERPDBName,CJDBName,ServiceIP FROM Ba_Account as a INNER JOIN Ba_Enterprise AS b ON a.EnterID = b.EnterID WHERE AccountName=@AccountName AND b.EnterNo=@EnterNo;";
            DataTable dtAccount = DbHelperSQL.Query(SqlString, hzyc_mes_mssqlStr, new SqlParameter[] {
                new SqlParameter("AccountName",type),
                new SqlParameter("EnterNo",code)
            }).Tables[0];
            string NewDataBaseName = dtAccount.Rows[0]["ERPDBName"].ToString();
            string CJDataBaseName = dtAccount.Rows[0]["CJDBName"].ToString();
            string ServiceIP = dtAccount.Rows[0]["ServiceIP"].ToString();
            DataBaseHelper.ERPDBName = NewDataBaseName;
            DataBaseHelper.CJDBName = CJDataBaseName;
            DataBaseHelper.ServiceIP = ServiceIP;

            if (!string.IsNullOrEmpty(NewDataBaseName))
            {
                //如果是走本地配置,读取配置文件信息
                if(ckEditNetWork.Checked)
                {
                    DataBaseHelper.IP = ConfigurationManager.AppSettings["IP"].ToString();
                    DataBaseHelper.SqlAccountName = ConfigurationManager.AppSettings["SqlAccountName"].ToString();
                    DataBaseHelper.SqlAccountPwd = ConfigurationManager.AppSettings["SqlAccountPwd"].ToString();
                    hzyc_mes_mssqlStr = "Data Source=" + DataBaseHelper.IP + ";Initial Catalog=" + NewDataBaseName + ";User Id=" + DataBaseHelper.SqlAccountName + ";Password=" + DataBaseHelper.SqlAccountPwd + "";
                }

                SqlString = "SELECT * FROM " + NewDataBaseName + ".dbo.SYS_User WHERE UserCode=@UserCode AND Password=@Password";
                password = DataConverter.MD5PWD(password);
                DataTable data = DbHelperSQL.Query(SqlString,hzyc_mes_mssqlStr,new SqlParameter[] { 
                    new SqlParameter("UserCode",username),
                    new SqlParameter("Password",password)
                }).Tables[0];
                if (data.Rows.Count > 0)
                {
                    //MessageBox.Show("登录成功");
                    form.Enabled = true;
                    //this.edtTo.Text = null;
                    //this.cb.Text = null;
                    this.ac.Text = null;
                    this.ps.Text = null;
                    this.Hide();
                    form.BringToFront();
                    form.Focus();
                    form.Show();
                    form.hideLogin(NewDataBaseName, data.Rows[0]["UserName"].ToString());
                    form.changeModulesNavigator();
                }
                else
                {
                    MessageBox.Show("登录失败,请输入或选择正确的信息！");
                }
            }
            else
            {
                MessageBox.Show("账套不可用,请登录有效账套！");
            }
        }

        private void sbOK_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                sbOK_Click(null, null);
            }
        }

        private void frmLogin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                sbOK_Click(null, null);
            }
        }
    }
}


